<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_AccountController extends Zend_Controller_Action 
{
	

	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			
			$this->_redirect(MY_URL.'/administrator-auth-login');
			
		}
		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('14', $strarray)) {
        		exit('no priviage');
        	}
		}
		
	}
	
	public function preDispatch()
	{
		
		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		//$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
		
	}
	
   	public function indexAction(){
    	
   		$account=new CAccount();
   		
    	$accountArray=$account->getAllAccounts(0,0);
    	
    	$receivedAccount=0;
    	
    	$givenSellerAccount=0;
    	
    	$givenBuyerAccount=0;
    	
    	$balance=0;
    	
    	foreach ($accountArray as $value){
    		
    		if($value['flid']=='1'){
    		
    			$receivedAccount+=$value['has_paid'];
    		
    		}else if($value['flid']=='2'){
    			
    			$givenSellerAccount+=$value['has_paid'];
    			
    		}else if($value['flid']=='3'){
    			
    			$givenBuyerAccount+=$value['has_paid'];
    			
    		}else if($value['flid']=='6'){
    			
    			$receivedAccount+=$value['has_paid'];
    			
    		}
    			
    	}
    	
    	$balance=$receivedAccount-$givenSellerAccount-$givenBuyerAccount;

		$this->view->assign ( 'receivedAccount', $receivedAccount );
		
		$this->view->assign ( 'givenSellerAccount', $givenSellerAccount );
		
		$this->view->assign ( 'givenBuyerAccount', $givenBuyerAccount );
		
		$this->view->assign ( 'balance', $balance );
		
		
		$disputeAccount=0;
		
		$actualAcount=0;
		
		$order=new COrder();
		
		$orderArray=$order->getAllOrders(0,0,0,R_PRE."orderfls.flid='6'");
		
		foreach ($orderArray as $value){
			
			$disputeAccount+=$value['money']*$value['discount'];
			
		}
		
		$disputeAccount=round($disputeAccount,2);
		
		$this->view->assign ( 'disputeAccount', $disputeAccount );
		
		//$actualAcount=$balance-$disputeAccount;
		
		//$this->view->assign ( 'actualAcount', $actualAcount );
   		
   	}
   	
   	public function itemAction(){
   		
		$page=$this->_request->getParam("page",1);
		
		$account=new CAccount();
		
		$counts=$account->getAllAccountCounts();
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $counts, 10 );
		
		$itemArray=$account->getAllAccounts($pageArray['from'],10);
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-account-item?"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
    	
    	$this->view->assign ( 'itemArray', $itemArray );
   		
   	}
   	
   	public function moreAction(){
   		
   		$accountid=$this->_request->getParam("accountid");
   		
   		$flid=$this->_request->getParam("flid");
   		
   		if($flid=='1'){
   		
	   		$account=new CAccount();
	   		
	   		$accountArray=$account->getDetail($accountid);
	   		
	   		$orderidArray=unserialize($accountArray[0]['orderid_string']);
	   		
	   		$order=new COrder();
	   		
	   		$orderArray=array();
	   		
	   		foreach ($orderidArray as $value){
	   			
	   			$orderArray[]=$order->getDetail($value);
	   			
	   		}
	   		
	   		$this->view->assign ( 'orderArray', $orderArray );
	   		
	   		$this->view->assign ( 'accountArray', $accountArray );
	   		
	   		
   		
   		}else if($flid=='2'){
   			
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
   			$member=new CMember();
   			
   			$memberArray=$member->getUserDetails(array('uid'=>$accountArray[0]['to_uid']));
   			
   			$this->view->assign ( 'memberArray', $memberArray );
   			
   		}else if($flid=='3'){
   			
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
   			$member=new CMember();
   			
   			$memberArray=$member->getUserDetails(array('uid'=>$accountArray[0]['to_uid']));
   			
   			$this->view->assign ( 'memberArray', $memberArray );
   			
   		}else if($flid=='4'){
   			
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$member=new CMember();
   			
   			$memberArray=$member->getUserDetails(array('uid'=>$accountArray[0]['from_uid']));
   			
   			$this->view->assign ( 'memberArray', $memberArray );
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
	   		$order=new COrder();
	   		
	   		$orderArray=$order->getAllOrders(0,0,0,R_PRE."orders.flid='9'",$memberArray[0]['uid']);
	   		
	   		$successAcount=0;
	   		
	   		foreach($orderArray as $value){
	   				
	   			$successAcount+=$value['money']*$value['discount'];
	   			
	   		}
	   		
	   		$successAcount=round($successAcount,2);
	   		
	   		$account=new CAccount();
	   		
	   		$accountArray=$account->getAllAccounts(0,0,"from_uid='".$memberArray[0]['uid']."' AND ".R_PRE."accounts.flid='4'");
	   		
	   		foreach ($accountArray as $value){
	   			
	   			$hasRequireAccount+=$value['should_paid'];
	   			
	   		}
	   		
	   		$canRequireAccount=0;
   		
   			$canRequireAccount=$successAcount-$hasRequireAccount;
   		
   			$this->view->assign('canRequireAccount',$canRequireAccount);
   			
   		}else if($flid=='5'){
   			
   			$account=new CAccount();
   			
   			$accountArray=$account->getDetail($accountid);
   			
   			$member=new CMember();
   			
   			$memberArray=$member->getUserDetails(array('uid'=>$accountArray[0]['from_uid']));
   			
   			$this->view->assign ( 'memberArray', $memberArray );
   			
   			$orderidArray=unserialize($accountArray[0]['orderid_string']);
   			
   			$this->view->assign ( 'orderidArray', $orderidArray );
   			
   			$this->view->assign ( 'accountArray', $accountArray );
   			
   			$order=new COrder();
   			
   			$orderArray=$order->getDetail($orderidArray[0]);
   			
   			$shouldReturn=round($orderArray[0]['money']*$orderArray[0]['discount'],2);
   			
   			$this->view->assign ( 'shouldReturn', $shouldReturn );
   			
   		}else if($flid=='6'){
   			
   			$account=new CAccount();
	   		
	   		$accountArray=$account->getDetail($accountid);
	   		
	   		$this->view->assign ( 'accountArray', $accountArray );
	   		
	   		$member=new CMember();
   			
   			$memberArray=$member->getUserDetails(array('uid'=>$accountArray[0]['from_uid']));
   			
   			$this->view->assign ( 'memberArray', $memberArray );
   			
   		}
   		
   		$this->render('more'.$flid);
   		
   	}
	
   	public function docompleteAction(){
   		
   		$this->_helper->viewRenderer->setNoRender();
   		
   		$accountid=$this->_request->getParam("accountid");
   		
   		$account=new CAccount();
   		
   		$accountArray=$account->getDetail($accountid);
   		
   		$account->editAccount($accountid,array('has_paid'=>$accountArray[0]['should_paid'],'state'=>1));
   		
   		$order=new COrder();
   		
   		$orderidArray=unserialize($accountArray[0]['orderid_string']);
   		
   		$order->editOrderArray($orderidArray,array('flid'=>3,'pay_time'=>time()));
   		
   		
   		$orderArray=$order->getDetail($orderidArray[0]);
		
		$mycoupon=new CCoupon();
			
		$mycoupon->subCoupon($orderArray[0]['coupon'],"WHERE uid='".$orderArray[0]['uid']."'");
   		
   		$this->view->showMsg("成功设置！");
   		
   	}
   	
   	public function virtualcompleteAction(){
   		
   		$this->_helper->viewRenderer->setNoRender();
   		
   		$accountid=$this->_request->getParam("accountid");
   		
   		$uid=$this->_request->getParam("uid");
   		
   		$type=$this->_request->getParam("type");
   		
   		$account=new CAccount();
   		
   		$accountArray=$account->getDetail($accountid);
   		
   		if($type==1){
   		
   			$account->editAccount($accountid,array('has_paid'=>$accountArray[0]['should_paid'],'state'=>1));
   			
   			$virtual=new CVirtual();
   			
   			$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
   			
   			$virtualMoney=(int)($accountArray[0]['should_paid']*$virtual_exchange_rate);
   			
   			$virtual->addVirtual($virtualMoney,"WHERE uid='".$uid."'");
   			
   		}else{
   			
   			$account->editAccount($accountid,array('has_paid'=>0,'state'=>0));
   			
   			$virtual=new CVirtual();
   			
   			$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
   			
   			$virtualMoney=(int)($accountArray[0]['should_paid']*$virtual_exchange_rate);
   			
   			$virtual->subVirtual($virtualMoney,"WHERE uid='".$uid."'");
   			
   		}
   		
   		$this->view->showMsg("成功设置！",$_SERVER['HTTP_REFERER']);
   		
   	}
   	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
